package com.example.demo.error;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * 全局异常捕捉
 * @author peixinxu
 *
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	
	
	@ExceptionHandler(value = Exception.class)
	@ResponseBody //返回如果是string或者是json则需要加上该注解
	public String defaultErrorHandle(HttpServletRequest req,Exception e) {
		return "哈哈，出错了，进入了自定义全局异常捕捉";
	}
	
	@ExceptionHandler(value = NullPointerException.class)
	public ModelAndView defaultNullPointerException(HttpServletRequest req,Exception e) {
		ModelAndView andView = new ModelAndView();
		andView.addObject("error", e);
		andView.setViewName("/error");
		return andView;
	}
	
}
